<?php
class ad {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function ad() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayListForm($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		
		if(isset($formvars['page']) && is_numeric($formvars['page'])){
			$page = $formvars['page'];
		}else{
			$page = 1;
		}
		
		$limit = "limit ".($page-1)*PAGE_SIZE.",".PAGE_SIZE;
		
		
		/**
		 * 総件数
		 */
	
		$sql = "SELECT COUNT(adspace_id) FROM adspace_mst";
		$stmt = $this->db->prepare($sql);	
		$stmt->execute();
		$count =$stmt->fetchColumn();
		$this->smarty->assign('count', $count);
		$stmt = null;
		
		/**
		 * 一覧
		 */
		
		$sql = "SELECT * FROM adspace_mst ORDER BY adspace_id  $limit";
		$stmt = $this->db->prepare($sql);
		$stmt->execute();

		$resultlist = $stmt->fetchAll();
		
		$stmt = null;
		
		$this->smarty->assign('resultlist', $resultlist);
		
		$this->smarty->assign('paging', $this->smarty->getNavigation("ad_index.php",$page,$count,''));
		$this->smarty->assign('msg', $this->msg);
		$this->initDropDown();

		$this->smarty->display( 'ad_index.tpl' );
	}
	
	function deletAd($formvars = array())
	{
		$adspace_id=$formvars['adspace_id'];
		$count =$this->db->exec("DELETE FROM adspace_mst WHERE adspace_id = '$adspace_id'");
		if($count>0)
		{
			$this->msg = "広告[$adspace_id]の削除に成功しました";
		}
		else
		{
			$this->msg = "広告[$adspace_id]の削除に失敗しました";
		}
		
	}
	
	function displayEditForm($formvars = array())
	{
		$adspace_id = $formvars['adspace_id'];
		$sql = "SELECT * FROM adspace_mst WHERE adspace_id = :adspace_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':adspace_id', $adspace_id);
		$stmt->execute();
		$result = $stmt->fetch();
		if($result != null)
		{
			$this->smarty->assign('rslt', $result);
		}
		else
		{
			$this->smarty->assign('fatal', true);
			$this->msg="広告[$adspace_id]が見つかりません";
		}
		$stmt = null;
		$this->smarty->assign('formvars',$formvars);
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'ad_edit.tpl' );
	}
	
	function doUpdate($formvars = array())
	{
		if(strpos($formvars['tag_contents'],'"'.$formvars['core_link'].'"')===false)
		{
			$this->msg = "広告タグの中身に指定する外部リンクを含んでいません";
			return;
		}
		$adspace_id = $formvars['adspace_id'];
		$sql = "Update adspace_mst set ad_name='".$formvars['ad_name']."',"
		."type=".$formvars['type'].","
		."type_c=".$formvars['type_c'].","
		."adult_ok=".$formvars['adult_ok'].","
		."open_flag=".$formvars['open_flag'].",tag_contents='".$formvars['tag_contents']."',"
		."core_link='".$formvars['core_link']."'" 
		." where adspace_id = $adspace_id";
		//echo $sql ;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "広告[$adspace_id]の更新に成功しました";
		}
		else
		{
			$this->msg = "広告[$adspace_id]を更新していません";
		}
		
	}
	
	function doInsert($formvars = array())
	{
		if(strpos($formvars['tag_contents'],'"'.$formvars['core_link'].'"')===false)
		{
			$this->msg = "広告タグの中身に指定する外部リンクを含んでいません";
			return;
		}
		$sql = "Insert into
		        adspace_mst(ad_name,type,type_c,adult_ok,open_flag,tag_contents,core_link,regist_date)
		        values('".$formvars['ad_name']."',".$formvars['type'].","
				.$formvars['type_c'].",".$formvars['adult_ok'].","
				.$formvars['open_flag'].",'".$formvars['tag_contents']."','"
				.$formvars['core_link']."',NOW())";
				
	    //echo $sql;
		 
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "広告の新規が完了しました";
		}
		else
		{
			$this->msg = "広告の新規ができません";
		}
		
	}
	
	function initDropDown()
	{		
		$this->smarty->assign('adTypeList', array(
                                '1' => 'PC',
                                '2' => 'モバイル'));
								
	    $this->smarty->assign('cTypeList', array(
								'0' => '該当なし',
                                '1' => 'DL10コース',
                                '2' => 'DL30コース',
								'3' => 'DL50コース'
								)
							);
		$this->smarty->assign('adultOkList', array(
                                '0' => 'ノンアダルト広告',
                                '1' => 'アダルト広告'));					
		$this->smarty->assign('openFlagList', array(
                                '1' => '公開',
                                '0' => '非公開'));
								
	}
	
}
?>